﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;
using System.Text;

public partial class Bidder_TraAccredit : System.Web.UI.Page
{
    public string _strProjectId;
    string UserCode, _strAccountId;
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumHospitale);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumSupervise);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAdmin);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumCompany);

        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            if (_Account != null)
            {
                _strProjectId = SysFun.IsSQL(Request.QueryString["ProjectId"].ToString());
                UserCode = _Account.UserCode;
                _strAccountId = _Account.AccountId.ToString();

                if (!IsPostBack)
                {
                    GetTransportation();
                }

                ShowSearchContent();
                if(dlPsr.Items.Count>0)
                {
                    if (dlPsr.SelectedValue == "0")
                    {
                        this.ButPrint.Enabled = false;
                    }
                    else 
                    {
                        this.ButPrint.Enabled = true;
                        this.ButPrint.Attributes.Add("onclick", "javascript:SkiptoPage("+_strProjectId+","+dlPsr.SelectedValue+")");
                    }
                }else
                {
                     this.ButPrint.Enabled = false;
                }
            }
        }
    }

    /// <summary>
    /// 显示查询内容
    /// </summary>
    private void ShowSearchContent()
    {
        string priceTitle = "挂网价";
        string fieldName = "";
        string field = "";
        switch (_strProjectId)
        {
            case "8":
                fieldName = "最高零售价";
                field = " ,retailPrice as " + fieldName;
                break;
            case "9":
                priceTitle = "最高零售价";
                break;
            case "10":
                priceTitle = "最高限价";
                fieldName = "最高零售价";
                field = " ,RetailPrice as " + fieldName;
                break;
            case "11":
                break;
            case "12":
                priceTitle = "最高零售价";
                break;

        }
        StringBuilder sbHtml = new StringBuilder();
        sbHtml.Append("<table width='100%' border='1' align='left' cellpadding='2' cellspacing='1' class='table_table2' id='Mytable'>");
        sbHtml.Append("<tr>");
        sbHtml.Append("<td  align='center' width='3%'>药品流水号</td>");
        sbHtml.Append("<td  align='center' width='8%'>通用名</td>");
        sbHtml.Append("<td  align='center'  width='5%'>商品名</td>");
        sbHtml.Append("<td  align='center'  width='5%'>剂型</td>");
        sbHtml.Append("<td  align='center'  width='3%'>规格</td>");
        sbHtml.Append("<td  align='center' width='5%'>转换<br/>系数</td>");
        sbHtml.Append("<td  align='center' width='3%'>包装<br/>规格</td>");
        sbHtml.Append("<td  align='center' width='6%'>质量<br/>层次</td>");

        sbHtml.Append("<td   width='6%'>" + priceTitle + "</td>");

        if (field != "")
        {
            sbHtml.Append("<td   width='6%'>" + fieldName + "</td>");
        }
        sbHtml.Append("<td  align='center' width='6%'>主配送人</td>");
        sbHtml.Append("<td  align='center' width='5%'>主配送人<br/>注册号</td>");
        sbHtml.Append("<td  align='center' width='9%'>生产企业</td>");
        sbHtml.Append("<td  align='center' width='5%'>生产企业<br/>注册号</td>");
        sbHtml.Append("</tr>");

        
        if (dlPsr.Items.Count > 0)
        {
            Db db = new Db();
            DbPage dbPage = new DbPage(db);
            dbPage.Table = "SupplyPlan_Goods";      //要查询的表明
            dbPage.Key = "SupplyGoodsId";     //表的主键    
            dbPage.Where = GetSqlFilter();     //查询条件    
            dbPage.SelectField = "SupplyGoodsId,ProductCode,goodsid,productname,goodsname,medicinemodel" + field + ",outlookc,factor," +
                                    "GoodsName,IsNull(Unit,'')+'/'+IsNull(PackUnit,'') As Pack1,"+
                                    "CASE QualityLevels when 0 then '普通GMP药品' when 1 then '专利药品' when 2 then '1999年以来获得国家科学技术奖的药品' when 3 then '国家一类新药' when 4 then '原研制药品' when 5 then '单独定价药品' when 6 then '优质优价中成药' when 7 then '全国医药工业百强企业的药品' when 8 then '中药保护品种' when 9 then '欧美认证药品' END AS QualityLevels ,convert( numeric(20,2), bidprice) as bidprice,companyname_ps,UserCode_PS," +
                                    "companyname_sc,UserCode_SC";//要显示的字段
            dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
            dbPage.PageSize = 30;                                                    //没有显示记录条数   
            dbPage.OrderBy = "ProductCode,ProductName,MedicineModel,Outlookc,SupplyGoodsId";//排序字段

            IDataReader dr=dbPage.ExecuteReader();
            int i=0;
            while(dr.Read())
            {
                sbHtml.Append("<tr class='table_td2'>");
                sbHtml.Append("<td  align='center' width='8%'>&nbsp;" + dr["goodsid"].ToString() + "</td>");
                sbHtml.Append("<td  align='center' width='8%'>&nbsp;" + dr["productname"].ToString() + "</td>");
                sbHtml.Append("<td  align='center' width='8%'>&nbsp;" + dr["goodsname"].ToString() + "</td>");
                sbHtml.Append("<td  align='center' width='8%'>&nbsp;" + dr["medicinemodel"].ToString() + "</td>");
                sbHtml.Append("<td  align='center' width='8%'>&nbsp;" + dr["outlookc"].ToString() + "</td>");
                sbHtml.Append("<td  align='center' width='8%'>&nbsp;" + dr["factor"].ToString() + "</td>");
                sbHtml.Append("<td  align='center' width='8%'>&nbsp;" + dr["Pack1"].ToString() + "</td>");
                sbHtml.Append("<td  align='center' width='8%'>&nbsp;" + dr["QualityLevels"].ToString() + "</td>");
                sbHtml.Append("<td width='6%'>" + dr["BidPrice"].ToString() + "</td>");

                if (field != "")
                {
                    sbHtml.Append("<td   width='6%'>" + dr[fieldName].ToString() + "</td>");
                }
               
                sbHtml.Append("<td  align='center' width='8%'>&nbsp;" + dr["companyname_ps"].ToString() + "</td>");
                sbHtml.Append("<td  align='center'width='8%'>&nbsp;" + dr["UserCode_PS"].ToString() + "</td>");
                sbHtml.Append("<td  align='center' width='8%'>&nbsp;" + dr["companyname_sc"].ToString() + "</td>");
                sbHtml.Append("<td  align='center' width='8%'>&nbsp;" + dr["UserCode_SC"].ToString() + "</td>");
                sbHtml.Append("</tr>");
                i++;
            }
            if (i == 0)
            {
                sbHtml.Append("<tr><td colspan='13' style='text-align:center;color:red;'>抱歉,没有查询到符合条件的信息!</td></tr></table>");
            }
            else
            {
                sbHtml.Append("</table>");
            }
            dr.Close();
            db.Close();
            this.DbPageFlip1.DbPage = dbPage;//将数据传给控键
          
        }
        else
        {
            sbHtml.Append("<tr><td colspan='13' style='text-align:center;color:red;'>抱歉,你还没有选择主配送人!</td></tr></table>");
        }
        
        Label1.Text = sbHtml.ToString();


    }

    /// <summary>
    /// 生成查询语句的条件
    /// </summary>
    /// <returns></returns>
    private string GetSqlFilter()
    {
        string psrID =dlPsr.SelectedItem.Value;
        string strGoodsid = txtGoodsId.Text.Trim();
        string strProductName = txtProductName.Text.Trim();
        string strMedcinemodel = txtMedicinemodel.Text.Trim();
        string strOutlookc = txtOutlookc.Text.Trim();
        string strFactor = txtFactor.Text.Trim();
        string strBidPrice = txtBidPrict.Text.Trim();
        string strGoodsName = txtGoodsName.Text.Trim();

        StringBuilder sbFilter = new StringBuilder(" accountid_tb="+_strAccountId+" and companyname_ps<>'' and isuse='1' and accountid_ps<>'0' and projectid="+_strProjectId);
        if (strGoodsid!="")
        {
            sbFilter.Append(" and goodsid like '%" + strGoodsid + "%'");
        }

        if (strProductName != "")
        {
            sbFilter.Append(" and (productname like '%" + strProductName + "%' or   Product_SpellingCode like '%" + strProductName + "%')");
        }
        if (strMedcinemodel != "")
        {
            sbFilter.Append(" and medicinemodel like '%" + strMedcinemodel + "%'");
        }
        if (strOutlookc != "")
        {
            sbFilter.Append(" and outlookc like '%" + strOutlookc + "%'");
        }
        if (strFactor != "")
        {
            sbFilter.Append(" and factor = '" + strFactor + "'");
        }
        if (strBidPrice != "")
        {
            sbFilter.Append(" and bidprice = '" + strBidPrice + "'");
        }

        if (strGoodsName != "")
        {
            sbFilter.Append(" and goodsname like '%" + strGoodsName + "%'");
        }
        if (psrID != "0")
        {
            sbFilter.Append(" and accountid_ps ='" + psrID + "'");
        }
        return sbFilter.ToString();
    }

    /// <summary>
    /// 绑定中标人的配送企业
    /// </summary>
    private void GetTransportation()
    {
        string sql = "select distinct accountid_ps,companyname_ps from dbo.SupplyPlan_Goods where accountid_tb=" + _strAccountId + " and ProjectId=" + _strProjectId + " and companyname_ps<>'' and accountid_ps<>'0'";
        Db db = new Db();
        IDataReader dr = db.DataReader(sql);
        this.dlPsr.Items.Clear();
        while(dr.Read())
        {
            string psrId = dr["accountid_ps"].ToString();
            string psrName = dr["companyname_ps"].ToString();
            dlPsr.Items.Add(new ListItem(psrName, psrId));
        }
        if (dlPsr.Items.Count > 1)
        {
            dlPsr.Items.Add(new ListItem("---全部---", "0"));
            dlPsr.SelectedIndex = 0;
        }

        dr.Close();
        db.Close();
    }
}
